<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    top="3px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="DepartmentName">
        <el-input v-model="form.DepartmentName" style="width: 200px" />
      </el-form-item>
      <el-form-item label="部门职位" prop="StaffPosts">
        <el-transfer
          v-model="form.StaffPosts"
          :data="StaffPostOptions"
          :titles="['选择职位', '已选']"
        />
      </el-form-item>
      <el-form-item label="负责行业" prop="Industrys">
        <el-transfer
          v-model="form.Industrys"
          :data="IndustryOptions"
          filterable
          :titles="['选择行业', '已选']"
        />
      </el-form-item>
      <el-form-item label="负责产品" prop="BusinessProducts">
        <el-cascader-panel
          v-model="form.BusinessProducts"
          style="width: fit-content; height: 350px"
          :props="{ multiple: true }"
          :options="optionsList"
        />
      </el-form-item>
      <!-- <el-form-item label="商机库容" prop="BusinessFullSet">
        <el-select v-model="form.BusinessFullSet" style="width: 200px">
          <el-option label="按系统总设置执行" :value="0" />
          <el-option label="按本部门设置执行" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.BusinessFullSet"
        label="公海商机"
        prop="OpenSeaBusinessNum"
      >
        <el-input v-model="form.OpenSeaBusinessNum" style="width: 100px" />
        <span style="margin-left: 10px">个</span>
      </el-form-item>
      <el-form-item
        v-if="form.BusinessFullSet"
        label="内海商机"
        prop="InSeaBusinessNum"
      >
        <el-input v-model="form.InSeaBusinessNum" style="width: 100px" />
        <span style="margin-left: 10px">个</span>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    getIndustrys,
    getStaffPosts,
    BusinessClassSelect,
  } from '@/api/common'
  import { doEdit } from '@/api/systemsetOrganizationDepartment'

  export default defineComponent({
    name: 'SystemsetOrganizationDepartmentEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        optionsList: [],
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
        StaffPostOptions: [],
        IndustryOptions: [],
      })

      const showEdit = (row) => {
        getMsg()
        state.form = Object.assign({}, row)
        if (row.DepartmentID == 0) {
          state.title = '添加'
          state.form.BusinessProducts = []
        } else {
          state.title = '编辑'
          if (row.BusinessProducts.length != 0) {
            state.form.BusinessProducts = row.BusinessProducts
          } else {
            state.form.BusinessProducts = []
          }
        }

        getStaffPost()
        getIndustry()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      const getStaffPost = async () => {
        const {
          data: { StaffPostOptions },
        } = await getStaffPosts()
        state.StaffPostOptions = StaffPostOptions
      }
      const getIndustry = async () => {
        const {
          data: { IndustryOptions },
        } = await getIndustrys()
        state.IndustryOptions = IndustryOptions
      }
      const getMsg = async () => {
        const {
          data: { BusinessClassOptions },
        } = await BusinessClassSelect()
        state.optionsList = BusinessClassOptions
        console.log('BusinessClassOptions', BusinessClassOptions)
      }

      return {
        ...toRefs(state),
        showEdit,
        getMsg,
        getIndustry,
        close,
        save,
      }
    },
  })
</script>
<style scoped>
  :deep(.el-scrollbar__wrap) {
    height: 350px;
  }
</style>
